<?php

/**
 *	user class
 *
 * 	this class provides the engine to manage users
 *	add users, modify users, delete users
 *
 *
 * 	@author			Pieter Van Peteghem <pieter@onedotonly.be>
 * 	@version		version 0.01 beta
 *
 * 	@copyright		Copyright (c) 2010, oneDot only <we@onedotonly.be>. All rights reserved.
 * 	@license		BSD License
 */
 
 
class CTRLuser extends CONTROL {


	/** 
	 * @later 	add user
	 */

	function __construct( $vars ) {
		global $DB, $Object;
		
		$this->_addvars( $vars );

		if( $this->action == "new" ) {
			
			$meta = array( 'title' => '...', 'fname' => '...', 'lname' => '...', 'mail' => '...', 'adress' => '...', 'pcode' => '...', 'city' => '...', 'gsm' => '...', 'birthd' => '...', 'birthm' => '...', 'birthy' => '...' );
			$input = array( 'user' => '...', 'pass' => '...', 'rank' => 1, 'meta' => $meta );
			$this->vars->id = $DB->set( 'user', $input );
			
			$this->vars->action = "edit";
			unset( $this->vars->floats );
			unset( $this->vars->blank );
			unset( $this->vars->descr );
			unset( $this->vars->flow );
			unset( $this->vars->reqs );
		
		} 		
			
		$Object = new OBJECT( $this->vars );
				
		return $Object;
		
	}
	
	
	function filter_new() {
		global $DB, $Object;
		
		if( $this->parent ) $new = "$('$this->parent').seed([" . $Object->render_core_json() . "], 0);";
		return $new . $this->filter_edit(); 

	}


#	Filter list 
	function filter_list() {
		global $Request;
	
		return "
			ref.seed([
			new Element( 'h1' ).update( '" . ucfirst( $this->vars->descr ) . "' ),
			lst = new List({ type: ref.vars.type, descr:'" . (( $this->vars->boxdescr )? $this->vars->boxdescr : $this->vars->descr ) . "', blank:'add {$this->vars->type}'})
		]);
		
		lst.seed( ". $Request->parse_list( "ID, ID as key_0, user as key_1, rank as key_2, active as key_3" ) ." );";
		
	
	}

#	List update.
	function filter_update_list() {
		global $Request;
		
		return "ref.seed( ". $Request->parse_list( "ID, rank, ID as key_0, user as key_1, active as key_2", $float = false ) ." );";
	}


#	Edit Object.
	function filter_edit() {
		global $Engine, $DB, $Object;
	
		// define user
		if( $User = $DB->get( 'user', 'ID = ' . $Object->id )) {
		
			// define meta content a user has
			if ( $meta = (array) json_decode( $User['meta'] )) {
				if( !$meta[ 'mail' ])
					$info .= "new Ajxfield({ type:'user', descr:'mail', ref:ref.id, id:{$Object->id}, field:'meta.mail', value:\"\", requestfunc:'pull' }),";
				foreach ($meta as $key => $val) {
					$info .= "new Ajxfield({ type:'user', descr:'$key', ref:ref.id, id:{$Object->id}, field:'meta.$key', value:\"$val\", requestfunc:'pull' }),";
				}
			} else {
		
				$metas = array( 'title', 'fname', 'lname', 'adress', 'pcode', 'city', 'gsm', 'birthd', 'birthm', 'birthy' );
				foreach( $metas as $meta ) {
					$info .= "new Ajxfield({ type:'user', descr:'$meta', ref:ref.id, id:{$Object->id}, field:'meta.$meta', value:'', requestfunc:'pull' }),";
				}	
			}
		}	
		
		return "
		ref.seed([
			
			new Element( 'h1' ).update( \"Beheer $Object->type '" . $User['user'] . "'\" ),
			new Options({ descr:'Account', active:true, floats: [
				new Ajxfield({ type:'user', descr:'username', ref:ref.id, id:{$Object->id}, field:'user', value:\"" . $User['user'] . "\", requestfunc:'username' }),
				new Ajxfield({ type:'user', descr:'level', ref:ref.id, id:{$Object->id}, field:'rank', value:'" . $User['rank'] . "', requestfunc:'level' }),
				new Shift({ type:'$Object->type', descr:'Is public', value:" . $User['active'] . ", field:'active', objid:{$Object->id} })
			]}),
			new Options({ descr:'Paswoord', floats: [
				new Ajxfield({ type:'user', descr:'new password', ref:ref.id, id:{$Object->id}, field:'pass', value:'•••••', requestfunc:'pass' })
			]}),
			new Options({ descr:'Extra settings', floats: [
				" . substr( $info, 0, -1 ) . "
			]})
		]);";
	}
	
	
	
#	Shift (alter column value).
	function filter_ajax() {
		global $DB, $Request;
		
		if( $Request->vars->requestfunc )
			$comment = $this->{ $Request->vars->requestfunc }();
		
		else 
			$comment = ( $Request->ajax() )? "option modified":"warning: failed";

		return $this->comment( $comment );
	}
	
	
	
#	User adaption in meta 
	function pull() {
		global $Request, $Object, $Engine, $DB;
	
		$DB->update( "user", $Object->id, array( 'meta' => array( $Request->vars->descr => $Request->vars->value )));
		//$DB->set( 'user', array( 'meta' => array( $Request->vars->descr => $Request->vars->value )), array( 'ID' => $Object->id ));
		
		return 'option modified';
				
	}
	

#	Password adaption
	function pass() {
		global $Request, $Object, $Engine, $DB;
	
		$DB->update( "user", $Object->id, array( 'pass' => $Request->vars->value ));
		//$DB->set( 'user', array( 'pass' => $Request->vars->value ), array( 'ID' => $Object->id ));
		return 'password modified';
				
	}
	
		
#	level adaption
	function level() {
		global $Request, $Object, $Engine, $DB;
	
		$DB->update( "user", $Object->id, array( 'rank' => $Request->vars->value ));
		//$DB->set( 'user', array( 'rank' => $Request->vars->value ), array( 'ID' => $Object->id ));
		return 'level modified';
				
	}
	
		
#	Username adaption
	function username() {
		global $Request, $Object, $Engine, $DB;
	
		$DB->update( "user", $Object->id, array( 'user' => $Request->vars->value ));
		//$DB->set( 'user', array( 'user' => $Request->vars->value ), array( 'ID' => $Object->id ));
		return 'username modified';
				
	}
	

}